#!@ND_PATH_BASH@
#
# Source:
#   https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids

CSV="alpn-protocol-ids.csv"
URL="https://www.iana.org/assignments/tls-extensiontype-values/${CSV}"

pushd() {
  command pushd "$@" > /dev/null
}

popd() {
  command popd "$@" > /dev/null
}

if [ -x "$(which wget)" ]; then
  pushd "@abs_top_builddir@/doc" || exit $?
  wget -qcN "${URL}" || exit $?
  popd
elif [ -x "$(which curl)" ]; then
  curl -s "${URL}" -z "@abs_top_builddir@/doc/${CSV}" -o "@abs_top_builddir@/doc/${CSV}" || exit $?
fi

if [ ! -f "@abs_top_builddir@/doc/${CSV}" ]; then
  exit 1
fi

cat << EOF
// Auto-generated by: $0

#pragma once

#include "nd-protos.hpp"

typedef unordered_map<
  const char *, nd_proto_id_t> nd_alpn_proto_map;

const nd_alpn_proto_map nd_alpn_protos = {
EOF

egrep -v '(^Proto|Reserved)' "@abs_top_builddir@/doc/${CSV}" |\
    sed \
        -e 's/["“”]*//g' \
        -e 's/^\(.*\),0x.*(\(.*\)),.*$/  { "\2\" \/* \1 *\/, NDPI_PROTO_UNKNOWN },/g'

cat << EOF
};

// vi: ei=all
EOF

exit 0
# vi: set ft=sh modelines=1 :
